﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace Mes.仓库管理.WareHouse
{
    public partial class WareHouseCreate : Form
    {
        public WareHouseCreate()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var warehouse = new
            {
                warehouseNo = txtWarehouseNo.Text,
                warehouseName = txtWarehouseName.Text,
                warehouseArea = selectWarehouseArea.Text,
                addrNo = txtAddrNo.Text,
                area = txtArea.Text,
                headPeople = txtHeadPeople.Text,
                states = true,
                plan = txtPlan.Text,
                remark = txtRemark.Text,
                //createPeople = Environment.UserName,
                //createTime = DateTime.UtcNow.ToString("o")
            };

            try
            {
                string json = JsonConvert.SerializeObject(warehouse);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.223.8.76:8085/api/WareHouse/CreateWarehouse");
                request.Method = "POST";
                request.ContentType = "application/json";

                byte[] byteData = Encoding.UTF8.GetBytes(json);
                request.ContentLength = byteData.Length;
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(byteData, 0, byteData.Length);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(stream))
                {
                    string responseText = reader.ReadToEnd();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        MessageBox.Show("仓库信息添加成功！");
                    }
                    else
                    {
                        MessageBox.Show($"添加失败，HTTP 状态码：{(int)response.StatusCode}, 错误信息：{responseText}");
                    }
                }
            }
            catch (WebException ex)
            {
                string errorText = "未知错误";
                if (ex.Response != null)
                {
                    using (WebResponse response = ex.Response)
                    {
                        HttpWebResponse httpResponse = (HttpWebResponse)response;
                        using (Stream stream = httpResponse.GetResponseStream())
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            errorText = reader.ReadToEnd();
                        }
                    }
                }
                MessageBox.Show($"添加失败，HTTP 状态码：{(int)((HttpWebResponse)ex.Response).StatusCode}, 错误信息：{errorText}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }
    }
}